﻿package com.hxh.ocean.view {
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.geom.Point;
	import com.hxh.ocean.helpers.*;
	import com.hxh.utils.*;
	
	import org.flintparticles.twoD.emitters.Emitter2D;
	
	public class SuperObject extends Sprite{
		
		// Constants:
		// Public Properties:
		// Private Properties:
		protected var _sprite:MovieClip;
		protected var _moveSpeed:Number;
		protected var _frameCount:uint;
		protected var _targetFrameCount:uint;
		protected var _visualEffects:Array;
	
		// Initialization:
		public function SuperObject(speed:Number=1) {
			_moveSpeed = speed;
			//Effects
			_visualEffects = new Array();
			//Hit effect, most likely all objects will have this effect
			//_visualEffects.push(EffectFactory.effect_GotHit());
		}
	
		/*********************** GETTERS / SETTERS *******************/		
		//Speed
		public function set MoveSpeed(s:Number):void {
			_moveSpeed=s;
		}
		public function get MoveSpeed():Number {
			return _moveSpeed;
		}
		
		//Effects
		public function get VisualEffects():Array{
			return _visualEffects;
		}
		
		/************************************************************/
		
		//Update the effects position etc
		protected function updateEffects():void{
			for (var i:uint = 0; i< _visualEffects.length; i++){
				_visualEffects[i].x = x;
				_visualEffects[i].y = y;
			}
		}
		//Stop all effects
		protected function killAllEffects():void{
			for (var i:uint = 0; i< _visualEffects.length; i++){
				_visualEffects[i].stop();
			}
		}
		//Stop a effect by index
		protected function killEffect(index:uint):void{
			_visualEffects[index].stop();
		}
		//Check collision with screen border									
		protected function checkBorderCollision(tuning:Number):int {
			var collideResult:int = Helper.COLLIDE_NONE;
			if (x>=Helper.SCREEN_WIDTH-tuning) {
				x=Helper.SCREEN_WIDTH-tuning;
				collideResult = Helper.COLLIDE_RIGHT;
			}
			if (y>=Helper.SCREEN_HEIGHT-tuning) {
				y=Helper.SCREEN_HEIGHT-tuning;
				collideResult = Helper.COLLIDE_BOTTOM;
			}
			if (x<=tuning) {
				x=tuning;
				collideResult = Helper.COLLIDE_LEFT;
			}
			if (y<=tuning) {
				y=tuning;
				collideResult = Helper.COLLIDE_TOP;
			}
			return collideResult;
		}
		
		//Stick self to a point
		protected function moveToPoint(destination:Point):void {
			var origin:Point=new Point(x,y);
			var slope:Number=Maths.slopeTwoPoints(origin,destination);

			if (slope>=0) {
				if (x<destination.x) {
					x+=MoveSpeed*Maths.cosineFromSlope(slope);
					y+=MoveSpeed*Maths.sineFromSlope(slope);
				} else {
					x-=MoveSpeed*Maths.cosineFromSlope(slope);
					y-=MoveSpeed*Maths.sineFromSlope(slope);
				}
			} else {
				if (x<destination.x) {
					x+=MoveSpeed*Maths.cosineFromSlope(slope);
					y+=MoveSpeed*Maths.sineFromSlope(slope);
				} else {
					x-=MoveSpeed*Maths.cosineFromSlope(slope);
					y-=MoveSpeed*Maths.sineFromSlope(slope);
				}
			}
		}
		
		//Move self along a line with a specific slope
		//The diffence from the above method is that it keeps moving in that direction
		//along the line and never stop
		protected function moveAlongALine(slope:Number, leftToRight:Boolean):void{
			if (slope>=0) {
				if (leftToRight) {
					x+=MoveSpeed*Maths.cosineFromSlope(slope);
					y+=MoveSpeed*Maths.sineFromSlope(slope);
				}
				if (!leftToRight) {
					x-=MoveSpeed*Maths.cosineFromSlope(slope);
					y-=MoveSpeed*Maths.sineFromSlope(slope);
				}
			} else {
				if (leftToRight) {
					x+=MoveSpeed*Maths.cosineFromSlope(slope);
					y+=MoveSpeed*Maths.sineFromSlope(slope);
				}
				if (!leftToRight) {
					x-=MoveSpeed*Maths.cosineFromSlope(slope);
					y-=MoveSpeed*Maths.sineFromSlope(slope);
				}
			}
		}
	}
}